package com.fengycx.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpStatus;
import com.fengycx.param.DriverConfirmOrderDto;
import com.fengycx.pojo.domain.Order;
import com.fengycx.pojo.dto.CreateOrderDto;
import com.fengycx.pojo.dto.OderBillDto;
import com.fengycx.pojo.dto.Order2DriverDto;
import com.fengycx.pojo.vo.OrderDetailVo;
import com.fengycx.pojo.vo.PointVo;
import com.fengycx.result.R;
import com.fengycx.service.IOrderService;
import com.fengycx.templeate.GTPushTemplate;
import com.fengycx.templeate.WechatJsapiPayTemplate;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/order")
public class AppOrderController {

    @Autowired
    public IOrderService orderService;

    @Autowired
    public GTPushTemplate gtPushTemplate;

    @Autowired
    public WechatJsapiPayTemplate wechatJsapiPayTemplate;
    /**
     *
     * @param headers
     * @param request
     * @param response
     * @return
     */
    @Operation( summary= "微信支付回调接口",description = "微信支付回调接口")
    @PostMapping("/wechat/pay/notify")
    @SaIgnore
    public R<Void> wechatPayNotify(@RequestHeader Map<String,String> headers, HttpServletRequest request, HttpServletResponse response){
        try {
            BufferedReader reader = request.getReader();
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null){
                stringBuilder.append(line);
            }
            String body = stringBuilder.toString();
            //得到回调解密后的response
            Transaction parse = wechatJsapiPayTemplate.parse(body, headers);


            response.setStatus(HttpStatus.HTTP_OK);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
            return R.error();
        }

    }

    //发送个推消息在 自定义事件业务逻辑里
    @Operation( summary= "个推绑定别名",description = "个推绑定别名接口")
    @Parameter(name = "cid",description = "调用个推得到的前端cid（每次得到的都不一样）",required = true)
    @PostMapping("/alias/bind/{cid}")
    public R<Void> gTBindAlias(@PathVariable("cid") @Valid String cid){
        gtPushTemplate.userAliasBind(cid, StpUtil.getLoginIdAsString());
        return R.success();

    }



    @Operation( summary= "司机发送账单信息",description = "司机推送账单信息接口")
    @Parameter(name = "orderNo",description = "订单号",required = true)
    @PostMapping("/driver/sendPayOrder/{orderNo}")
    public R<Order> driverSendPayOrder(@PathVariable("orderNo") @Valid String orderNo){
        orderService.driverSendPayOrder(orderNo);
        return R.success();

    }

    @Operation( summary= "司机查询账单信息",description = "司机查询账单信息接口")
    @Parameter(name = "orderNo",description = "订单号",required = true)
    @PostMapping("/driver/consult/{orderNo}")
    public R<OderBillDto> driverConsultOrder(@PathVariable("orderNo") @Valid String orderNo){
        OderBillDto orderBilldto = orderService.payOrderSend(orderNo);
        return R.success();

    }

    @Operation( summary= "司机确认费用",description = "司机确认费用接口")
    @Parameter(name = "param",description = "额外费用和订单号",required = true)
    @PostMapping("/confirm")
    public R<Void> driverConfirmFee(@RequestBody DriverConfirmOrderDto confirmDto){
        orderService.driverConfirmOrder(confirmDto);
        return R.success();

    }

    @Operation( summary= "司乘同显页面乘客获取订单详情",description = "乘客获取订单详情接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @GetMapping("/customer/getOrderDetail/{orderNo}")
    public R<OrderDetailVo> getOrderDetail(@PathVariable("orderNo") @Valid String orderNo){
        OrderDetailVo detail=  orderService.getOrderDetail(orderNo);
        return R.success(detail);

    }

    @Operation( summary= "实时拉取司机坐标",description = "实时拉取司机坐标接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @PostMapping("/pullDriverLocation/{orderNo}")
    public R<PointVo> pullDriverLocation(@PathVariable("orderNo") @Valid String orderNo){
        PointVo point=  orderService.pullDriverLocation(orderNo);
        return R.success(point);

    }

    @Operation( summary= "根据订单编号查询订单",description = "根据订单编号查询订单接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @PostMapping("/info/{orderNo}")
    public R<Order> orderInfo(@PathVariable("orderNo") @Valid String orderNo){
        return R.success(orderService.orderInfo(orderNo));

    }

    @Operation( summary= "乘客创建order订单",description = "乘客创建order订单接口")
    @Parameter(name = "createOrderParam",description = "乘客创建order订单的对象",required = true)
    @PostMapping("/createOrder")
    public R<String> createOrder(@RequestBody @Valid CreateOrderDto createOrderParam){
        if(R.success().isSuccess()){
            return R.success(orderService.createOrder(createOrderParam));
        }
     else {
         return R.error();
        }
    }

    @Operation( summary= "乘客取消订单",description = "乘客取消订单接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @PostMapping("/cancelOrder/{orderNo}")
    public R<Void> cancelOrder(@PathVariable("orderNo") @Valid String orderNo){
            orderService.cancelOrder(orderNo);
            return R.success();

    }

    @Operation( summary= "查询乘客正在进行中的订单",description = "查询乘客正在进行中的订单接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @GetMapping("/queryProcessingOrder")
    public R<Order> queryProcessingOrder(){
            return R.success(orderService.queryProcessingOrder());
    }

    @Operation( summary= "司机拉取订单",description = "司机拉取订单接口")
    @GetMapping("/driver/pullOrder2Driver")
    public R<List<Order2DriverDto>> pullOrder2Driver(){
        List<Order2DriverDto> orderList=orderService.pullOrder2Driver();
            return R.success(orderList);
    }

    @Operation( summary= "司机抢单",description = "司机抢单接口")
    @Parameter(name = "orderNo",description = "订单编号",required = true)
    @PostMapping("/driver/grabOrder/{orderNo}")
    public R<Order> grabOrder(@PathVariable("orderNo")@Valid String orderNo){
        Order order=orderService.grabOrder(orderNo);
        return R.success(order);
    }

    @Operation( summary= "回显司机抢单成功后的订单",description = "回显司机抢单成功后的订单")
    @GetMapping("/driver/reshowProcessingOrder")
    public R<Order> reshowProcessingOrder(){
        Order  order =orderService.reshowProcessingOrder();
        return R.success(order);
    }

    @Operation( summary= "司机到达代驾点",description = "司机到达代驾点接口")
    @GetMapping("/driver/arriveStartPlaceHandle")
    public R<Order> arriveStartPlaceHandle(){
        Order  order =orderService.arriveStartPlaceHandle();
        return R.success(order);
    }

    @Operation( summary= "司机开始代驾",description = "司机开始代驾接口")
    @GetMapping("/driver/startDrivingHandle")
    public R<Order> startDrivingHandle(){
        Order  order =orderService.startDrivingHandle();
        return R.success(order);
    }

    @Operation( summary= "司机结束代驾",description = "司机结束代驾接口")
    @GetMapping("/driver/endDrivingHandle")
    public R<Order> endDrivingHandle(){
        Order  order =orderService.endDrivingHandle();
        return R.success(order);
    }

}
